*************************************************************
* Replication codes for analyses in Section 4 (on corn) of
* Beyond Yield Response: Weather Shocks and Crop Abandonment
* May 2020
*************************************************************

	clear
	gen initial = .	
	
	save dta\boot_bote_corn, replace
	
	set seed 123456
	
	forvalues t = 1/1000 {
	
	use dta\bote_population_pcws, clear
	
	qui keep if prod_corn!=.

	forvalues t= 5(5)20 {
		forvalues k=8(3)38 {
			qui gen d`k'_`t' = rgs_dd3bin`k'_bote`t' - rgs_dd3bin`k'_bote0
			}
		}	
	
	gen stateansi = floor(fips/1000)
	
	bsample 1572
	
* bote calculations

	* spline reg
	forvalues t = 5(5)20 {
	
	gen spline_corn_loss_ha_`t' = 0.0145 *(gdd8_26_bote`t' - gdd8_26_bote0) + ///
							-0.0005 *(gdd26_35_bote`t' - gdd26_35_bote0) + ///
							-0.0018 *(dday35C_bote`t' - dday35C_bote0)

	gen spline_corn_loss_yld_`t' = 0.0778 *(gdd8_26_bote`t' - gdd8_26_bote0) + ///
							-0.0038 *(dday26C_bote`t' - dday26C_bote0) 
																					 
	replace spline_corn_loss_ha_`t' = 1 - acres_grain_ratio_harvest if spline_corn_loss_ha_`t' + acres_grain_ratio_harvest > 1
	replace spline_corn_loss_yld_`t' = -1 if spline_corn_loss_yld_`t'<-1	
	}

	forvalues t = 5(5)20 {
	gen spline_corn_ttloss_yld`t' = (spline_corn_loss_yld_`t')*pl_acre_corn
	gen spline_corn_ttloss_ha`t' = (spline_corn_loss_ha_`t')*pl_acre_corn		
	}
	
	* tAvg reg
	forvalues t = 5(5)20 {
	gen tAvg_corn_loss_ha_`t' = 0.0239*(tAvg_S1a_bote`t' - tAvg_S1a_bote0) -0.0010*(tAvg_S1a_bote`t'^2 - tAvg_S1a_bote0^2) + ///
							 0.1128*(tAvg_S2a_bote`t' - tAvg_S2a_bote0) -0.0027*(tAvg_S2a_bote`t'^2 - tAvg_S2a_bote0^2)	
	
	gen tAvg_corn_loss_yld_`t' = 0.1434*(tAvg_S1a_bote`t' - tAvg_S1a_bote0) -0.0049*(tAvg_S1a_bote`t'^2 - tAvg_S1a_bote0^2) + ///
							 0.4445*(tAvg_S2a_bote`t' - tAvg_S2a_bote0) -0.0122*(tAvg_S2a_bote`t'^2 - tAvg_S2a_bote0^2)	
							 
	replace tAvg_corn_loss_ha_`t' = 1 - acres_grain_ratio_harvest if tAvg_corn_loss_ha_`t' + acres_grain_ratio_harvest > 1
	replace tAvg_corn_loss_yld_`t' = -1 if tAvg_corn_loss_yld_`t'<-1	
	}

	forvalues t = 5(5)20 {
	gen tAvg_corn_ttloss_yld`t' = (tAvg_corn_loss_yld_`t')*pl_acre_corn
	gen tAvg_corn_ttloss_ha`t' = (tAvg_corn_loss_ha_`t')*pl_acre_corn		
	}
	
	* bin reg
	forvalues t = 5(5)20 {
	gen bin_corn_loss_ha_`t' = .0402*d8_`t' -.2968*d11_`t' +.6545*d14_`t' -.0421*d17_`t' +.2711*d20_`t' + ///
							.4228*d23_`t' + .6529*d26_`t' -.0962*d29_`t' -.2806*d32_`t' -.7460*d35_`t' -3.0320*d38_`t'
		
	gen bin_corn_loss_yld_`t' = .1113*d8_`t' -2.2661*d11_`t' +2.1373*d14_`t' -.1613*d17_`t' +.3319*d20_`t' + ///
							2.6452*d23_`t' + 2.5389*d26_`t' -1.6315*d29_`t' -4.6102*d32_`t' -6.7804*d35_`t' -4.2395*d38_`t'
							
	replace bin_corn_loss_ha_`t' = 1 - acres_grain_ratio_harvest if bin_corn_loss_ha_`t' + acres_grain_ratio_harvest > 1
	replace bin_corn_loss_yld_`t' = -1 if bin_corn_loss_yld_`t'<-1	
	}

	forvalues t = 5(5)20 {
	gen bin_corn_ttloss_yld`t' = (bin_corn_loss_yld_`t')*pl_acre_corn
	gen bin_corn_ttloss_ha`t' = (bin_corn_loss_ha_`t')*pl_acre_corn		
	}							
	
	qui collapse (sum) *ttloss* pl_acre*
	
	foreach x in spline tAvg bin {
	forvalues t = 5(5)20 {
	gen `x'_corn_shrloss_yld`t' = `x'_corn_ttloss_yld`t'/pl_acre_corn
	gen `x'_corn_shrloss_ha`t' = `x'_corn_ttloss_ha`t'/pl_acre_corn		
	gen `x'_corn_loss_overest`t' = 1-(`x'_corn_shrloss_ha`t'+`x'_corn_shrloss_yld`t')/`x'_corn_shrloss_yld`t'
	}
	}
		
	keep *corn_shr* *corn_loss*
	
	save dta\corn_boot_temp, replace
	use dta\boot_bote_corn, clear
	append using dta\corn_boot_temp
	
	save dta\boot_bote_corn, replace
	}

	use dta\boot_bote_corn, clear
	
	sum *corn_shrloss_ha* 
	sum *corn_shrloss_yld* 
	
	sum *corn_loss_overest*
	
	